local Controller = Class()

-- style of the controller: Normal(default), PopupBox, Stick
-- Normal(default): when this ui show, other will hide
-- PopupBox: when this ui show, other is still show, and also, can interactive

Controller.StylePopupBox = false
Controller.StyleStick = false     --this isn't used when use UGUI

-- properties
Controller.IsOpen = false
Controller.IsShow = false
Controller.IsInteractive = false

Controller.ControllerName = nil
Controller.ViewName = nil
Controller.View = nil
Controller.TimerCd = nil
Controller.SubCtrl = { }

function Controller:init(ctrlName, viewName)
    self.ControllerName = ctrlName
    self.ViewName = viewName
    UIManager.registerController(self)
end

function Controller:sendNtfMsg(ntfType, ...)
    UIManager.sendNtfMessage(ntfType, ...)
end

function Controller:creat()
    if self.View ~= nil and self.View.UI ~= nil then
        return
    end
    if self.ViewName ~= nil then
        self.View =(require(self.ViewName))
        self.View:LoadView()
    end
    self:onCreat()

    for k, v in pairs(self.SubCtrl) do
        v:creat()
    end

    --local aliveTime = UIDisposeConfig[self.ControllerName] or 0
    --if nil == self.TimerCd and aliveTime ~= -1 then
    --    self.TimerCd = TimerManager.newTimer(aliveTime, false, true, nil, nil, function(t) t:destroy(false) end, self)
    --end
end

function Controller:open(data)
    self.IsOpen = true

    self:onOpen(data)

    for k, v in pairs(self.SubCtrl) do
        v:open(data)
    end

    UIManager.pushingStack(self)

    if nil ~= self.TimerCd then
        self.TimerCd:pause()
    end
end

function Controller:close()
    UIManager.popingStack(self)

    for k, v in pairs(self.SubCtrl) do
        v:close()
    end
    self:onClose()

    self.IsOpen = false

    if nil ~= self.TimerCd then
        self.TimerCd:start()
    end
end

function Controller:show()
    self.IsShow = true

    if self.View ~= nil and not self.View:isDispose() then
        self.View:show()
        self:onShow()
    end

    for k, v in pairs(self.SubCtrl) do
        v:show()
    end
end

function Controller:interactive(active)
    self.IsInteractive = active

    for k, v in pairs(self.SubCtrl) do
        v:interactive(active)
    end
    if self.View ~= nil and not self.View:isDispose() then
        self.View:interactive(active)
        self:onInteractive(active)
    end
end

function Controller:hide()
    self.IsShow = false

    for k, v in pairs(self.SubCtrl) do
        v:hide()
    end

    if self.View ~= nil and not self.View:isDispose() then
        self.View:hide()
        self:onHide()
    end
end

function Controller:destroy(enforces)

    if self.IsOpen then
        UIManager.popingStack(self)
    end

    for k, v in pairs(self.SubCtrl) do
        v:destroy(enforces)
    end

    if self.View ~= nil then
        self:onDestroy()
        self.View:destroy()
    end

    if nil ~= self.TimerCd then
        TimerManager.disposeTimer(self.TimerCd)
    end

    self.IsOpen = false
    self.IsShow = false
    self.TimerCd = nil
    self.View = nil
end

function Controller:sortingOrder(order)

    for k, v in pairs(self.SubCtrl) do
        v:sortingOrder(order)
    end
    if self.View ~= nil and not self.View:isDispose() then
        self.View:sortingOrder(order)
    end
end

function Controller:ntfHandle(ntfType, ...)

    for k, v in pairs(self.SubCtrl) do
        v:ntfHandle(ntfType, ...)
    end
    self:onNtfHandle(ntfType, ...)
end

function Controller:update()

    for k, v in pairs(self.SubCtrl) do
        v:update()
    end
    self:onUpdate()
end

-- override functions
function Controller:onNtfHandle(ntfType, ...)
    Debug.Log("Controller: onNtfHandle, should be inherited")
end

function Controller:onCreat()
    Debug.Log("Controller: onCreat, should be inherited")
end

function Controller:onOpen(data)
    Debug.Log("Controller: onOpen, should be inherited")
end

function Controller:onClose()
    Debug.Log("Controller: onClose, should be inherited")
end

function Controller:onShow()
    Debug.Log("Controller: onShow, should be inherited")
end

function Controller:onInteractive(active)
    Debug.Log("Controller: onInteractive, should be inherited")
end

function Controller:onHide()
    Debug.Log("Controller: onHide, should be inherited")
end

function Controller:onUpdate()
    Debug.Log("Controller: onUpdate, should be inherited")
end

function Controller:onDestroy()
    Debug.Log("Controller: onDestroy, should be inherited")
end

return Controller

